package com.example.administrator.zhongyichou.base;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.utils.LogUtils;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.view.LoadingPage;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by lichenyu on 2017/8/10.
 */

public abstract class BaseFragment<T extends GsonBaseProtocol> extends Fragment {

    protected LoadingPage mLoadingPage;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        if (mLoadingPage == null) {

            mLoadingPage = new LoadingPage(getActivity()) {
                @Override
                protected boolean requestNetWorkData() {
                    return isRequestData();
                }

                @Override
                public View createSuccessView() {
                    return initContentView();
                }

                @Override
                public void loadData() {
                    requestData();
                }
            };
        }
        mLoadingPage.showHead(isTitle());
        return mLoadingPage;
    }

    /**
     * 默认是需要请求网络数据,如果页面不需要请求网络,可以重写这个方法
     *
     * @return true 需要请求网络数据 false 不需要请求网络数据
     */
    protected boolean isRequestData() {
        return false;
    }

    protected void requestData() {
        HttpLoader mHttpLoader = initHttpLoader();      //1.创建好Retrofit对象,将协议类传递给HttpLoader,并且将HttpLoader类引用传递过来
        setRequestParams(mHttpLoader);                  //2.设置请求参数,将HttpLoader作为参数传递,
        if (mHttpLoader == null)
            return;
        mHttpLoader.executor(new ResultListener<T>() {  //3.执行去请求网络,里面自定义一个接口封装状态
            @Override
            public void success(T mGsonBaseProtocol) {
                mLoadingPage.showSuccessPage();
                try {
                    responseData(mGsonBaseProtocol);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    mLoadingPage.showEmptyPage();
                    ToastUtil.showToast("内部程序异常");
                    LogUtils.e(e);
                }
            }

            @Override
            public void error(String mErrorProtocal) {
                mLoadingPage.showEmptyPage();
            }

            @Override
            public void notNet() {
                if (mLoadingPage != null)
                    mLoadingPage.showErrorPage();
            }

            @Override
            public void requestError() {
                mLoadingPage.showErrorPage();
            }
        });
    }

    /**
     * 设置参数，如果需要，重写此方法.
     * <p>
     * 传递HttpLoader加载器类
     *
     * @param mHttpLoader
     */
    protected void setRequestParams(HttpLoader mHttpLoader) {

    }

    /**
     * 具体填充的逻辑有子类实现
     *
     * @param mGsonBaseProtocal
     */
    protected abstract void responseData(T mGsonBaseProtocal);

    /**
     * 子类自己去初始化相应的loader加载器类
     *
     * @return
     */
    protected abstract HttpLoader initHttpLoader();

    protected abstract View initContentView();

    //获取状态栏高度
    public int getStatusBarHeight() {
        int result = 0;
        if (Build.VERSION.SDK_INT >= 19) {
            result = 0;
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = getResources().getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //响应eventbus事件
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(BaseEventBean baseEventBean) {
        if (onEventBus(baseEventBean)) {
            EventBus.getDefault().removeStickyEvent(baseEventBean);
        }
    }

    public abstract boolean onEventBus(BaseEventBean baseEventBean);

    /**
     * fragment默认不显示失败页面的头
     * @return
     */
    public boolean isTitle() {
        return false;
    }
}